package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-06-23
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;
    @Override
    //根据课程id删除小结
    //TODO:删除小结，删除对应的视频
    public void removeVideoServiceCourseId(String courseId) {
        //根据课程id查询出所有的视频id
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);

        //变成list<String>
        List<String> videos = new ArrayList<>();
        eduVideos.forEach(eduVideo -> {
            if (eduVideo != null && !StringUtils.isEmpty(eduVideo.getVideoSourceId())){
                videos.add(eduVideo.getVideoSourceId());
            }
        });
        if (!videos.isEmpty()){
            vodClient.removeAlyVideoBatch(videos);
        }

        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id", courseId);
        baseMapper.delete(eduVideoQueryWrapper);
    }
}
